﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOC_Conception_Simple_Example
{
    class Program
    {
        static void Main(string[] args)
        { 

            new IOCContainer();
            IDbDAL dbDAL = new PostgreSqlDAL();
            new User(dbDAL)
            {
                Username = "SkilledProgrammer"
            }.Save(); 
            Console.Read();
        }
    }
    public class User
    {
        public string Username { get; set; }
        /// <summary>
        /// 使用接口是个不错的主意！
        /// </summary>
        [Inject]
        public IDbDAL _dbDal { get; set; }

      
        public User(IDbDAL dbDal)
        {
            _dbDal = dbDal;
        }

        public void Save()
        { 
            _dbDal.Add(this);
        }
    }

    public interface IDbDAL
    {
        void Add(User user);
    }
    public class MySQLDAL : IDbDAL
    {
        public void Add(User user)
        {
            Console.WriteLine($"{user.Username} has been saved into mysql db");
        }
    }
    public class PostgreSqlDAL : IDbDAL
    {
        public void Add(User user)
        {
            Console.WriteLine($"{user.Username} has been saved into postgres db");
        }
    }
}
